package master;
import java.util.Comparator;

import common.WorkerObject;

public class SortWorkers implements Comparator<WorkerObject> {
	
	final static String ACS = "ACS";
	final static String DECS = "DECS";
	
	private String type;

	private String state;

	public SortWorkers(String type, String state) {
		this.state = state;
		this.type = type;
	}

	public SortWorkers() {

	}

	public int compare(WorkerObject o1, WorkerObject o2) {
		if (state.equals(DECS)) {
			System.out.println("DECS");
			if (type.matches("PRICE")) return descendingSortPrice(o1, o2);
			else return descendingSortLoad(o1, o2);
		} else if (state.equals(ACS)) {
			System.out.println("ACS");
			if (type.matches("PRICE")) return ascendingSortPrice(o1, o2);
			return ascendingSortLoad(o1, o2);
		} else {
			System.out.println("Not a valid param.");
			return 0;
		}
	}

	// sort the object in descending way
	private int descendingSortLoad(WorkerObject o1, WorkerObject o2) {
		if (o1.getLoad() < o2.getLoad()) {
			return 1;
		} else if (o1.getLoad() > o2.getLoad()) {
			return -1;
		} else
			return 0;
	}

	// sort the object in ascending way
	private int ascendingSortLoad(WorkerObject o1, WorkerObject o2) {
		if (o1.getLoad() > o2.getLoad()) {
			return 1;
		} else if (o1.getLoad() < o2.getLoad()) {
			return -1;
		} else
			return 0;
	}
	
	// sort the object in descending way
	private int descendingSortPrice(WorkerObject o1, WorkerObject o2) {
		if (o1.getPrice() < o2.getPrice()) {
			return 1;
		} else if (o1.getPrice() > o2.getPrice()) {
			return -1;
		} else
			return 0;
	}

	// sort the object in ascending way
	private int ascendingSortPrice(WorkerObject o1, WorkerObject o2) {
		if (o1.getPrice() > o2.getPrice()) {
			return 1;
		} else if (o1.getPrice() < o2.getPrice()) {
			return -1;
		} else
			return 0;
	}
}
